package controllers

import (
	"Wc_work/models"
	"fmt"
	"log"

	"github.com/astaxie/beego"
	"github.com/astaxie/beego/orm"
)

type MainController struct {
	beego.Controller
}

func (c *MainController) Get() {
	c.TplName = "login.html"
}

func (c *MainController) ShowLogin() {
	c.TplName = "login.html"
}

var userzh int

//登录业务处理
func (c *MainController) HandleLogin() {
	//1.拿到数据
	userName, _ := c.GetInt("username")
	beego.Info(userName)
	pwd := c.GetString("password")
	beego.Info(pwd)
	beego.Info("成功获取用户数据")

	// 2.查询账号密码是否正确
	o := orm.NewOrm()
	//根据账号查询密码
	r := o.Raw("select wc__pwd from user where wc__id = ?", userName)
	var rpwd string
	r.QueryRow(&rpwd)
	beego.Info(rpwd)
	if rpwd != pwd {
		log.Print("用户名或密码不正确")
		c.TplName = "login.html"
		return
	}

	//3.跳转
	beego.Info("本次登录成功")
	//根据账号查询身份
	r2 := o.Raw("select Wc__Employee_type from user where wc__id = ?", userName)
	var rEmp string
	r2.QueryRow(&rEmp)
	//根据身份跳转对应路由
	if rEmp == "主管" {
		c.Redirect("/manage", 302)
	} else {
		userzh = userName
		c.Redirect("/user", 302)
	}
	c.Ctx.WriteString("登录成功，欢迎回来！")
}

func (c *MainController) ShowManage() {
	o := orm.NewOrm() //导入orm
	//定义结构体数组
	var Wc_users []models.User
	_, err := o.QueryTable("user").All(&Wc_users)
	if err != nil {
		beego.Info("查询信息出错")
		return
	}
	c.Data["Wc_users"] = Wc_users
	c.TplName = "manage.html"
}
func (c *MainController) ShowManageselect() {
	o := orm.NewOrm()
	id, _ := c.GetInt("q")
	//定义结构体数组
	var Wc_users []models.User
	_, err := o.QueryTable("user").Filter("Wc_id", id).All(&Wc_users)
	if err != nil {
		beego.Info("查询信息出错")
		return
	}
	c.Data["Wc_users"] = Wc_users
	c.TplName = "manage.html"
}

func (c *MainController) ShowManage_1() {
	c.TplName = "manage-1.html"
}

func (c *MainController) Showaa() {
	//修改资料的get方法
	o := orm.NewOrm()
	id, _ := c.GetInt("q")
	//定义结构体数组
	var Wc_users []models.User
	_, err := o.QueryTable("user").Filter("Wc_id", id).All(&Wc_users)
	if err != nil {
		beego.Info("查询信息出错")
		return
	}
	c.Data["Wc_users"] = Wc_users
	c.TplName = "manage-1-1.html"
}
func (c *MainController) HandleManage_1() {
	//修改治疗的post方法
	o := orm.NewOrm()
	id, _ := c.GetInt("id")
	pwd := c.GetString("pwd")
	name := c.GetString("name")
	sex := c.GetString("sex")
	emp := c.GetString("emp")
	ctime, _ := c.GetInt("ctime")
	dep := c.GetString("dep")
	pos := c.GetString("pos")
	time := c.GetString("time")
	Wc_user := models.User{}
	Wc_user.Wc_Id = id
	Wc_user.Wc_Pwd = pwd
	Wc_user.Wc_Name = name
	Wc_user.Wc_Sex = sex
	Wc_user.Wc_Employee_type = emp
	Wc_user.Wc_classTime = ctime
	Wc_user.Wc_Department = dep
	Wc_user.Wc_post = pos
	if pos == "部门主管" {
		Wc_user.Wc_salary = 8000 + ctime*20
	} else if pos == "部门经理" {
		Wc_user.Wc_salary = 7000 + ctime*20
	} else {
		Wc_user.Wc_salary = 5000 + ctime*20
	}
	Wc_user.Wc_Entry_date = time
	num, err := o.Update(&Wc_user)
	if err != nil {
		beego.Info("更新失败")
	} else {
		beego.Info("更新数据影响的行数:", num)
	}
	c.TplName = "manage-1.html"
}

func (c *MainController) ShowManage2() {
	//页面二的get方法
	o := orm.NewOrm()
	//定义结构体数组
	var Wc_users1 []models.User
	_, err := o.QueryTable("user").Distinct().All(&Wc_users1, "Wc_Department")
	if err != nil {
		beego.Info("查询信息出错")
		return
	}
	c.Data["Wc_users1"] = Wc_users1
	c.TplName = "manage2.html"
}
func (c *MainController) ShowManage2select() {
	//页面二的post方法，根据部门查找该部门员工
	o := orm.NewOrm()
	dep := c.GetString("q2")
	beego.Info(dep)
	//定义结构体数组
	var Wc_users []models.User
	_, err := o.QueryTable("user").Filter("Wc_Department", dep).All(&Wc_users)
	if err != nil {
		beego.Info("查询信息出错")
		return
	}
	c.Data["Wc_users"] = Wc_users
	c.TplName = "manage2-2.html"
}
func (c *MainController) ShowManage3() {
	c.TplName = "manage3.html"
}
func (c *MainController) ShowManage4() {
	o := orm.NewOrm() //导入orm
	//定义结构体数组
	var Wc_users []models.User
	_, err := o.QueryTable("user").All(&Wc_users)
	if err != nil {
		beego.Info("查询信息出错")
		return
	}
	c.Data["Wc_users"] = Wc_users
	c.TplName = "manage4.html"
}
func (c *MainController) HandleManage3() {
	//获取页面数据，用于修改数据库数据
	o := orm.NewOrm()
	pwd := c.GetString("pwd")
	name := c.GetString("name")
	sex := c.GetString("sex")
	emp := c.GetString("emp")
	id, _ := c.GetInt("id")
	dep := c.GetString("dep")
	ctime, _ := c.GetInt("ctime")
	pos := c.GetString("pos")
	time := c.GetString("time")
	r2 := o.Raw("select Wc__id from user where wc__id = ?", id)
	var rEmp int
	var sal int
	r2.QueryRow(&rEmp)
	if pos == "部门主管" {
		sal = 8000
	} else if pos == "部门经理" {
		sal = 7000
	} else {
		sal = 5000
	}

	if rEmp == id {
		c.Ctx.WriteString("插入失败!员工编号重复")
	} else {
		var user = models.User{Wc_Id: id, Wc_Pwd: pwd, Wc_Name: name, Wc_Sex: sex, Wc_Employee_type: emp, Wc_classTime: ctime, Wc_Department: dep, Wc_post: pos, Wc_Entry_date: time, Wc_salary: sal}
		_, i2 := o.Insert(&user)
		if i2 != nil {
			c.Ctx.WriteString("插入失败！" + fmt.Sprint(i2))
		} else {
			c.Ctx.WriteString("插入成功!")
		}
	}

}

func (c *MainController) ShowUser() {
	o := orm.NewOrm()
	//定义结构体数组
	var Wc_users []models.User
	_, err := o.QueryTable("user").Filter("Wc_id", userzh).All(&Wc_users)
	if err != nil {
		beego.Info("查询信息出错")
		return
	}
	c.Data["Wc_users"] = Wc_users
	c.TplName = "user.html"
}

func (c *MainController) ShowUser2() {
	o := orm.NewOrm()
	//定义结构体数组
	var Wc_users []models.User
	_, err := o.QueryTable("user").Filter("Wc_id", userzh).All(&Wc_users)
	if err != nil {
		beego.Info("查询信息出错")
		return
	}
	c.Data["Wc_users"] = Wc_users
	c.TplName = "user2.html"
}

func (c *MainController) HandleUser() {
	o := orm.NewOrm()
	pwd := c.GetString("pwd")
	name := c.GetString("name")
	sex := c.GetString("sex")
	emp := c.GetString("emp")
	ctime, _ := c.GetInt("ctime")
	dep := c.GetString("dep")
	pos := c.GetString("pos")
	time := c.GetString("time")
	Wc_user := models.User{}
	Wc_user.Wc_Id = userzh
	Wc_user.Wc_Pwd = pwd
	Wc_user.Wc_Name = name
	Wc_user.Wc_Sex = sex
	Wc_user.Wc_Employee_type = emp
	Wc_user.Wc_classTime = ctime
	Wc_user.Wc_Department = dep
	Wc_user.Wc_post = pos
	if pos == "部门主管" {
		Wc_user.Wc_salary = 8000 + ctime*20
	} else if pos == "部门经理" {
		Wc_user.Wc_salary = 7000 + ctime*20
	} else {
		Wc_user.Wc_salary = 5000 + ctime*20
	}
	Wc_user.Wc_Entry_date = time
	num, err := o.Update(&Wc_user)
	if err != nil {
		beego.Info("更新失败")
	} else {
		beego.Info("更新数据影响的行数:", num)
	}
	c.TplName = "user.html"
}
